package org.jeecg.modules.front.pay;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.front.util.YinShengUtil;
import org.jeecg.modules.yinsheng.YinShengClient;
import org.jeecg.modules.yinsheng.command.base.HeadResponseCommand;
import org.jeecg.modules.yinsheng.command.base.MerchantHeadRequestCommand;
import org.jeecg.modules.yinsheng.command.jspay.JsPayReqCommand;
import org.jeecg.modules.yinsheng.command.jspay.JsPayRespCommand;
import org.jeecg.modules.yinsheng.command.scancode.FrontCodePayReqCommand;
import org.jeecg.modules.yinsheng.command.scancode.FrontCodePayRespCommand;
import org.jeecg.modules.yinsheng.common.ServiceEnum;
import org.jeecg.modules.yinsheng.common.YsPayConstant;
import org.jeecg.modules.yinsheng.utils.JsonParser;
import org.jeecg.modules.yinsheng.utils.YinshengLogger;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.UUID;

@Service
public class YinshengJsScan extends Payment{


    public YinshengJsScan(String channel) {
        // 构造方法体
        this.channel = channel;

    }

    public YinshengJsScan() {
        // 构造方法体

    }

    public Result<JSONObject> pay(String requestNo, Double amount, Map<String, String> extendParams)
    {

        // 先构建请求客户端
        // 先构建请求客户端
        YinShengClient yinShengClient = new YinShengClient(ServiceEnum.JS_PAY, YsPayConstant.ENV_PRD, YinShengUtil.getPrdMerchantPrx(), YinShengUtil.getPrdMerchantPrxPwd(), YinShengUtil.getRsaPrd());
        MerchantHeadRequestCommand headRequestCommand = new MerchantHeadRequestCommand().setSrcMerchantNo(YinShengUtil.getPrdSrcMerchantNo());
        headRequestCommand.setServiceNo(ServiceEnum.WALLET_PAY_FRONT_CODE.getApiName());
        headRequestCommand.setServiceNo(ServiceEnum.JS_PAY.getApiName());
        JsPayReqCommand jsPayReqCommand = new JsPayReqCommand();
        jsPayReqCommand.setRequestNo(requestNo);//M	业务请求号
        jsPayReqCommand.setPayeeMerchantNo(YinShengUtil.getPrdPayeeMerchantNo());;//收款方银盛商户号
        jsPayReqCommand.setOrderDesc((extendParams.get("name")+"*"+extendParams.get("num")));;//商品名称/订单标题
        jsPayReqCommand.setAmount(String.valueOf(amount));//交易金额，单位：元取值范围：0.01-999999.99
        jsPayReqCommand.setBankType("1902000");//支付渠道1902000-微信(支持payMode：28、29)1903000-支付宝(支持payMode：26)9001002-银联(支持payMode：30)
        jsPayReqCommand.setPayMode("28");//支付方式26-支付宝生活号 、28-微信公众号29-微信小程序、30-银联行业码支付
        jsPayReqCommand.setWxAppId(YinShengUtil.getWAppID());//商户在微信侧申请的小程序或公众号APPIDpayMode=28、29时必填
        jsPayReqCommand.setWxOpenId(extendParams.get("wxOpenId"));//付款方微信openId，payMode=28、29时必填
        //支付宝参数
		/*jsPayReqCommand.setAlipayId("123456");//付款方支付宝userIdpayMode=26时，不能与alipayLogonId同时为空
		jsPayReqCommand.setAlipayLogonId("13344");//付款方支付宝账号payMode=26时，不能与alipayId同时为空*/
        //银联参数
        /*jsPayReqCommand.setUnionUserId("123455");//付款方银联userId，通过获取用户标识接口返回payMode=30时必填*/
        //非必填参数
        jsPayReqCommand.setNotifyUrl(YinShengUtil.getNotifyUrl()+"front/yinsheng/yinsheng/notify");;//支付成功结果异步通知地址，为空则不通知支持多个url，url之间用分隔符“,”分开
        HeadResponseCommand baseResponseCommand = yinShengClient.doAction(headRequestCommand, jsPayReqCommand);
        YinshengLogger.logBizInfo(baseResponseCommand);
        JsPayRespCommand respCommand = JsonParser.parserJsonStringToJavaObject(baseResponseCommand.getBizResponseJson(),
                JsPayRespCommand.class);
        String str= JsonParser.parserJavaObjectToJsonString(respCommand, false);
        JSONObject jsonObject = JSON.parseObject(str);
        if(jsonObject.get("subCode").equals("COM000")||jsonObject.get("subCode").equals("COM004"))
        {
            return Result.ok(jsonObject);
        }
        else
        {
            return Result.error(String.valueOf(jsonObject.get("subMsg")));
        }

    }


    public  Integer payType()
    {
        if(this.channel.equals("wechat"))
        {
            return 4;
        }


        return 0;
    }
}
